package com.coolgor.coldot.dao.auth;

import java.util.List;

import com.coolgor.coldot.entity.auth.OperationTrace;
import org.apache.ibatis.annotations.*;


@Mapper
public interface OperationTraceDao {

	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_auth_operationtrace
			<where>
				<if test="operationTraceCondition.recordID != null">
					and record_id = #{operationTraceCondition.recordID}
				</if>			
				<if test="operationTraceCondition.userID != null">
					and user_id = #{operationTraceCondition.userID}
				</if>
				<if test="operationTraceCondition.username != null">
					and username like '%${operationTraceCondition.username}%'
				</if>
				<if test="operationTraceCondition.nameCN != null">
					and name_cn like '%${operationTraceCondition.nameCN}%'
				</if>		
				<if test="operationTraceCondition.roleID != null">
					and role_id = #{operationTraceCondition.roleID}
				</if>
				<if test="operationTraceCondition.roleName != null">
					and role_name = #{operationTraceCondition.roleName}
				</if>
				<if test="operationTraceCondition.source != null">
					and source = #{operationTraceCondition.source}
				</if>
				<if test="operationTraceCondition.target != null">
					and target = #{operationTraceCondition.target}
				</if>
				<if test="operationTraceCondition.actionTime != null">
					and action_time = #{operationTraceCondition.actionTime}
				</if>
				<if test="operationTraceCondition.lastTime != null">
					and last_time = #{operationTraceCondition.lastTime}
				</if>
				<if test="operationTraceCondition.operation != null">
					and operation like '%${operationTraceCondition.operation}%'
				</if>
				<if test="operationTraceCondition.remark != null">
					and remark like '%${operationTraceCondition.remark}%'
				</if>	
			</where>
			ORDER BY action_time DESC
			LIMIT #{rowIndex},#{pageSize};		
		</script>
	""")
	@Results(id = "OperationTraceMap", value = {
			@Result(property = "recordID", column = "record_id"),
			@Result(property = "userID", column = "user_id"),
			@Result(property = "username", column = "username"),
			@Result(property = "nameCN", column = "name_cn"),
			@Result(property = "roleID", column = "role_id"),
			@Result(property = "roleName", column = "role_name"),
			@Result(property = "source", column = "source"),
			@Result(property = "target", column = "target"),
			@Result(property = "actionTime", column = "action_time"),
			@Result(property = "lastTime", column = "last_time"),
			@Result(property = "operation", column = "operation"),
			@Result(property = "remark", column = "remark")
	})
	List<OperationTrace> queryOperationTraceList(@Param("operationTraceCondition") OperationTrace operationTraceCondition,
												 @Param("rowIndex") int rowIndex,
												 @Param("pageSize") int pageSize);


	@Select("""
		<script>		
			SELECT
				count(1)
			FROM
				tb_coldot_auth_operationtrace
			<where>
				<if test="operationTraceCondition.recordID != null">
					and record_id = #{operationTraceCondition.recordID}
				</if>			
				<if test="operationTraceCondition.userID != null">
					and user_id = #{operationTraceCondition.userID}
				</if>
				<if test="operationTraceCondition.username != null">
					and username like '%${operationTraceCondition.username}%'
				</if>
				<if test="operationTraceCondition.nameCN != null">
					and name_cn like '%${operationTraceCondition.nameCN}%'
				</if>		
				<if test="operationTraceCondition.roleID != null">
					and role_id = #{operationTraceCondition.roleID}
				</if>
				<if test="operationTraceCondition.roleName != null">
					and role_name = #{operationTraceCondition.roleName}
				</if>
				<if test="operationTraceCondition.source != null">
					and source = #{operationTraceCondition.source}
				</if>
				<if test="operationTraceCondition.target != null">
					and target = #{operationTraceCondition.target}
				</if>
				<if test="operationTraceCondition.actionTime != null">
					and action_time = #{operationTraceCondition.actionTime}
				</if>
				<if test="operationTraceCondition.lastTime != null">
					and last_time = #{operationTraceCondition.lastTime}
				</if>
				<if test="operationTraceCondition.operation != null">
					and operation like '%${operationTraceCondition.operation}%'
				</if>
				<if test="operationTraceCondition.remark != null">
					and remark like '%${operationTraceCondition.remark}%'
				</if>	
			</where>		
		</script>
	""")
	int queryOperationTraceCount(@Param("operationTraceCondition") OperationTrace operationTraceCondition);


	@Insert("""
		<script>
			INSERT INTO tb_coldot_auth_operationtrace
				(user_id,username,name_cn,role_id,role_name,source,target,
				action_time,last_time,operation,remark)
			VALUES
				(#{userID},#{username},#{nameCN},#{roleID},#{roleName},#{source},#{target},
				#{actionTime},#{lastTime},#{operation},#{remark})			
		</script>
	""")
	@Options(useGeneratedKeys = true, keyColumn = "record_id", keyProperty = "recordID")
	int insertOperationTrace(OperationTrace operationTrace);


	@Update("""
		<script>
			UPDATE
				tb_coldot_auth_operationtrace
			<set>
				<if test="userID != null">user_id=#{userID},</if>
				<if test="username != null">username=#{username},</if>
				<if test="nameCN != null">name_cn=#{nameCN},</if>
				<if test="roleID != null">role_id=#{roleID},</if>
				<if test="roleName != null">role_name=#{roleName},</if>
				<if test="source != null">source=#{source},</if>
				<if test="target != null">target=#{target},</if>
				<if test="actionTime != null">action_time=#{actionTime},</if>
				<if test="lastTime != null">last_time=#{lastTime},</if>
				<if test="operation != null">operation=#{operation},</if>
				<if test="remark != null">remark=#{remark}</if>
			</set>
			WHERE
				record_id = #{recordID}			
		</script>
	""")
	int updateOperationTrace(OperationTrace operationTrace);


	@Delete("""
		<script>
			DELETE FROM
				tb_coldot_auth_operationtrace
			WHERE
				record_id = #{recordID}			
		</script>
	""")
	int deleteOperationTrace(OperationTrace operationTrace);


	@Delete("""
		<script>
			DELETE FROM
				tb_coldot_auth_operationtrace
			WHERE record_id IN
			<foreach collection="list" item="recordID" open="(" separator="," close=")">
				#{recordID}
			</foreach>			
		</script>
	""")
	int batchDeleteOperationTrace(List<Long> recordIDList);

}
